<?php
/*
	插件概览		
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'welcome';
$tabs=array(
	array('op'=>'welcome','title'=>'概况'),
	array('op'=>'line','title'=>'趋势'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='welcome'){
	//会员
	$order=array(
		'num'=>pdo_count('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid)),
		'order_price'=>pdo_getsum('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),'price'),
		'member'=>$this->count('member',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid)),
		'get_money'=>$this->count('orderlog',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),'get_money'),
	);
	$return['order']=$order;
	$return['htmls'][]=array('type'=>'data','one_num'=>3,'title'=>'订单概况','datas'=>array(
		array('title'=>'总订单数','num'=>1,'data'=>$order['num']),
		array('title'=>'总订单额','num'=>1,'data'=>$order['order_price']),
		array('title'=>'总配送员','num'=>1,'data'=>$order['member']),
		array('title'=>'总配送费','num'=>1,'data'=>$order['get_money']),
	));
	//佣金
	$data=array(
		'num'=>pdo_count('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid)),
		'ordernum'=>$this->count('orderlog',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),'order_price'),
		'access'=>pdo_count('core_access_log',array('uniacid'=>$_SESSION['uniacid'],'plugin'=>'jy_peimilk')),
	);
	$return['data']=$data;
	$return['htmls'][]=array('type'=>'data','one_num'=>4,'title'=>'数据概况','datas'=>array(
		array('title'=>'总用户','num'=>1,'data'=>$data['num']),
		array('title'=>'总成交量','num'=>1,'data'=>$data['ordernum']),
		array('title'=>'总访问统计','num'=>1,'data'=>$data['access']),
	));
	
	$order_pie=array(
		'title'=>'订单概况',
		'tablename'=>$this->tablename('orderlog'),
		'width'=>'50%',
		'field'=>'status',
		'field_options'=>$this->send_status,
		'params'=>array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),
	);
	$return['htmls'][]=model('core/echart')->pie($order_pie);
	$fee_pie=array(
		'title'=>'配送费概况',
		'tablename'=>$this->tablename('orderlog'),
		'width'=>'50%',
		'field'=>'type',
		'field_options'=>$this->feetypes,
		'params'=>array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),
	);
	$return['htmls'][]=model('core/echart')->pie($fee_pie);
	//计算待结算金额
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>3);
	$ids=pdo_getall('jy_weishop_order',$params,'id','id');
	$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'clearstatus'=>0);
	if($ids){
			$ids=array_keys($ids);
			$params['orderid']=$ids;
	}
	$order_goods=pdo_getall('jy_weishop_order_goods',$params,array('id','orderid','total_price','merchid','goodsid','optionid','fee_desc'));
	$clearfee0=0;
	$commission_money0=0;
	foreach($order_goods as &$gv){
				$order=pdo_get('jy_weishop_order',array('id'=>$gv['orderid']),array('ordersn','createtime','dispatchprice','peisong_id'));
				$gv['ordersn']=$order['ordersn'];
				$gv['createtime']=$order['createtime'];
				$gv['ordergoodsid']=$gv['id'];
				$gv['id']=$gv['orderid'];
				//商品规格
				if($gv['optionid']){
					$specs=pdo_getcolumn('jy_weishop_goods_option',array('id'=>$optionid),'specs');
					$specs=explode("_",$specs);
					foreach($specs as &$spec){
						$spec=pdo_getcolumn('jy_weishop_goods_spec_item',array('id'=>$spec),'title');
					}
				}
				if(!$gv['fee_desc']){
					model('jy_weishop/order')->setfee_desc($gv['orderid']);
					$gv['total_price']=pdo_getcolumn('jy_weishop_order_goods',array('id'=>$gv['id']),'total_price');
				}
				$total_price=$gv['total_price'];
				if($order['peisong_id']){
					$total_price -=$order['dispatchprice'];
				}
				$clearfee0 +=$total_price;
				//计算佣金
				if($this->plugin_exists('jy_commission')){
						$gv['commission_money']=model('jy_commission/commission')->get_goodsfee($gv['goodsid'],$gv['orderid'],$gv['ordergoodsid']);
						$commission_money0 +=$gv['commission_money'];
				}
	}
	$return['clearfee0']=price($clearfee0);
	$return['commission_money0']=price($commission_money0);
	$_SESSION['logout_forward']='jy_peimilk/login/';
	if($_SESSION['os']!='windows'){
		$return['htmls']='';
		$return['item']=$merch;
		$return['item']['goodsnum']=pdo_count('jy_weishop_goods',array('merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']),'id');
		$return['item']['logo']=tomedia($return['item']['logo']);
		$return['isopenvoice']=$return['item']['isopenvoice'];
		$return['merchs']=pdo_getall('jy_merch_user',array('mid'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']));
		$avatar=pdo_getcolumn('core_members',array('id'=>$return['item']['mid']),'avatar');
		$return['item']['logo']=$return['item']['logo']?$return['item']['logo']:$avatar;
		$return['item']['url']=custom_url('jy_merch/merch//id='.$merchid,true);
		$return['item']['payurl']=custom_url('jy_merch/member.recharge//merchid='.$merchid,true);
		$params=array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid,'deleted'=>0);
		$params['status']=1;
		$return['status1']=pdo_count('jy_weishop_order',$params);
		$params['status']=0;
		$return['ispay']=pdo_count('jy_weishop_order',$params);
		$params['status']=2;
		$return['status2']=pdo_count('jy_weishop_order',$params);
		$return['setting']=$this->setting;
	}
	$params=array('sendtype'=>1,'merchid'=>$_SESSION['onemerchid'],'uniacid'=>$_SESSION['uniacid']);
	$params['status']=0;
	$status0=pdo_count('jy_peimilk_sendlog',$params);
	$params['status']=1;
	$status1=pdo_count('jy_peimilk_sendlog',$params);
	$params['status']=2;
	$status2=pdo_count('jy_peimilk_sendlog',$params);
	$params['status']=7;
	$status7=pdo_count('jy_peimilk_sendlog',$params);
	$params['status']=0;
	$params['day']=$day;
	$statusmday=pdo_count('jy_peimilk_sendlog',$params);
	$params['day']=date('Y-m-d');
	$params['status']=2;
	$today_num=pdo_count('jy_peimilk_sendlog',$params);
	$params['status']=array(0,1,2);
	$statusday=pdo_count('jy_peimilk_sendlog',$params);
	$sendlog=pdo_getall('jy_peimilk_sendlog',array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$_SESSION['onemerchid'],'status'=>2));
	$goods_num=0;
	$allgoods_num=0;
	foreach($sendlog as $sv){
		$sv['goods']=json_decode($sv['goods'],true);
		foreach($sv['goods'] as $gv){
			if($sv['createtime']>$daytime){
				$goods_num +=$gv['num'];
			}
			$allgoods_num +=$gv['num'];
		}
	}
	$return['data']=array(
		'status0'=>$status0,
		'status1'=>$status1,
		'status2'=>$status2,
		'status7'=>$status7,
		'statusmday'=>$statusmday,
		'statusday'=>$statusday,
		'today_num'=>$today_num,
		'allgoods_num'=>$allgoods_num,
	);
	set_html('title','概况');
	exi($return);
}
if($_RQ['op']=='line'){
	//趋势
	$order_params=array(
		'title'=>'近30天订单走势',
		'data'=>array(
			'tablename'=>$this->tablename('orderlog'),
		),
		'params'=>array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),
	);
	$return['htmls'][]=model('core/echart')->line($order_params);
	$member_params=array(
		'title'=>'近三十天会员/配送员增长走势',
		'data'=>array(
			'会员增长'=>array(
				'tablename'=>'jy_weishop_member',
			),
			'配送员增长'=>array(
				'tablename'=>$this->tablename('member'),
				'params'=>array('uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid),
			)
		),
	);
	$return['htmls'][]=model('core/echart')->line($member_params);
	set_html('title','配送员趋势');
	exi($return);
}